package com.pet.service.impl;

import com.pet.constant.MessageConstant;
import com.pet.constant.StatusConstant;
import com.pet.context.BaseContext;
import com.pet.dto.admin.adminData.EmployeeDTO;
import com.pet.dto.web.EmployeeLoginDTO;
import com.pet.entity.Employee;
import com.pet.exception.AccountNotFoundException;
import com.pet.exception.PasswordErrorException;
import com.pet.mapper.EmployeeMapper;
import com.pet.service.EmployeeService;
import com.pet.vo.system.adminData.EmployeeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }


        //3、返回实体对象
        return employee;
    }

    /**
     * 获取管理员信息
     * @return
     */
    public EmployeeVO getAdminInfo() {
        Employee employee = employeeMapper.getAdminInfo(BaseContext.getCurrentId());
        EmployeeVO employeeVO = new EmployeeVO();
        BeanUtils.copyProperties(employee,employeeVO);
        return employeeVO;
    }

    /**
     * 修改管理员信息
     * @param employeeVO
     */
    public void updateAdminInfo(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employeeMapper.updateAdminInfo(employee);
        log.info("修改管理员信息：{}",employee);
    }

    /**
     * 新增员工
     * @param employeeDTO
     */
//    public void save(EmployeeDTO employeeDTO) {
//        //对象属性拷贝
//        Employee employee = new Employee();
//        BeanUtils.copyProperties(employeeDTO,employee);
//
//        //设置账号状态，默认正常状态
//        employee.setStatus(StatusConstant.ENABLE);
//
//        //设置密码，默认123456
//        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
//
//        //设置时间，创建时间和修改时间
//        //employee.setCreateTime(LocalDateTime.now());
//        //employee.setUpdateTime(LocalDateTime.now());
//
//        //设置当前记录创建者的id
//        //employee.setCreateUser(BaseContext.getCurrentId());
//        //employee.setUpdateUser(BaseContext.getCurrentId());
//
//        employeeMapper.insert(employee);
//    }
//
//    /**
//     * 分页查询方法
//     * @param employeePageQueryDTO
//     * @return
//     */
//    public PageResult pageQueery(EmployeePageQueryDTO employeePageQueryDTO) {
//        //开始分页查询
//        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
//
//        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
//
//        long total = page.getTotal();
//        List<Employee> records = page.getResult();
//
//        return new PageResult(total, records);
//    }
//
//    /**
//     * 启用或禁用员工账号
//     * @param status
//     * @param id
//     */
//    public void startOrStop(Integer status, Long id) {
//        //使用builder进行创建对象
//        Employee employee = Employee.builder()
//                .status(status)
//                .id(id)
//                .build();
//
//        employeeMapper.update(employee);
//    }
//
//    /**
//     * 通过id进行查询
//     * @return
//     */
//    public Employee getByid(Long id) {
//        Employee employee = employeeMapper.getById(id);
//        //保护密码
//        employee.setPassword("****");
//        return employee;
//    }
//
//    /**
//     * 编辑员工信息
//     * @param employeeDTO
//     */
//    public void update(EmployeeDTO employeeDTO) {
//        //传入的是Employee对象
//        Employee employee = new Employee();
//        //将对象的内容复制到另一个对象中
//        BeanUtils.copyProperties(employeeDTO, employee);
//
//        //设置时间和操作者
//        //employee.setUpdateTime(LocalDateTime.now());
//        //employee.setUpdateUser(BaseContext.getCurrentId());
//
//        log.info("获得的对象：{}",employee);
//
//        //调用相关的方法
//        employeeMapper.update(employee);
//    }

}
